package com.animal.service.impl;

import com.animal.entity.AnAnimal;
import com.animal.entity.AnCollect;
import com.animal.entity.AnComment;
import com.animal.entity.AnUser;
import com.animal.mapper.AnAnimalMapper;
import com.animal.mapper.AnCollectMapper;
import com.animal.mapper.AnInvitationMapper;
import com.animal.mapper.AnUserMapper;
import com.animal.service.AnCollectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author animal
 * @since 2022-01-29
 */
@Service
public class AnCollectServiceImpl extends ServiceImpl<AnCollectMapper, AnCollect> implements AnCollectService {
    @Autowired
    AnCollectMapper anCollectMapper;
    @Autowired
    AnAnimalMapper anAnimalMapper;
    @Autowired
    AnInvitationMapper anInvitationMapper;
    @Autowired
    AnUserMapper anUserMapper;

    @Override
    public List<AnCollect> findAll(int size,int current) {

        Page<AnCollect> userPages = new Page<>(size,current);
        Page<AnCollect> userPagess=  anCollectMapper.selectPage(userPages,null);
        List<AnCollect> list=userPagess.getRecords();
       for(AnCollect a:list){
           if(a.getAnId()!=null){
               a.setAnAnimal(anAnimalMapper.selectById(a.getAnId()));
           }
          if(a.getInId()!=null){
              a.setAnInvitation(anInvitationMapper.selectById(a.getInId()));
          }
          if(a.getUserId()!=null){
              a.setAnUser(anUserMapper.selectById(a.getUserId()));
          }

       }
        return list;
    }

    @Override
    public List<AnCollect> findByUserId(int size, int current, int userId) {
        Page<AnCollect> userPages = new Page<>(size,current);
        QueryWrapper<AnCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Page<AnCollect> userPagess=  anCollectMapper.selectPage(userPages,wrapper);
        List<AnCollect> list=userPagess.getRecords();
        for(AnCollect a:list){
            if(a.getAnId()!=null){
                a.setAnAnimal(anAnimalMapper.selectById(a.getAnId()));
            }
            if(a.getInId()!=null){
                a.setAnInvitation(anInvitationMapper.selectById(a.getInId()));
            }
            if(a.getUserId()!=null){
                a.setAnUser(anUserMapper.selectById(a.getUserId()));
            }

        }
        return list;
    }
}
